Skip to content

Fix generics and some formatting - #59

Open
dwalluck wants to merge 1 commit into
Commonjava:masterfrom
dwalluck:generics-cleanup
Open

Fix generics and some formatting#59
dwalluck wants to merge 1 commit into
Commonjava:masterfrom
dwalluck:generics-cleanup

Conversation

@dwalluck

Copy link
Copy Markdown
Contributor

No description provided.

public AbstractJiraServerInfo parse( Object object )
{
Map<String, Object> map = (Map<String, Object>) object;
Map<?, ?> map = (Map<?, ?>) object;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it would be better to use @SuppressWarnings( "unchecked" ) here rather than the wildcard parameterised type ; this would then match your changes in e.g. Registry.java and ParseUtils.

@dwalluck dwalluck Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have a simple way to explain it. The problem is where the cast is (first line vs. second line):

Wrong way:

Map<String, Integer> map = (Map<String, Integer>) obj; // wrong: unchecked cast
Integer i = map.get( "x" ); // compiler can't verify this, can throw ClassCastException

Right way:

Map<?, ?> map = (Map<?, ?>) obj;
Integer i = (Integer) map.get( "x" ) // checked cast here

When I use @SuppressWarnings("unchecked") in Registry and ParseUtils, that was where it was safe.

In other words, cast the get not the Map.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #48.

@rnc rnc Aug 26, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full disclosure - Bob helped here!
You're right in that the general principal is valid ( i.e. Casting individual values is safer than casting the container ). However every value is already cast individually below. So changing the map declaration from Map<String, Object> to Map adds nothing to safety.
What the change does introduce is an asymmetry: render() in the same class still declares Map<String, Object> on line 49, since it constructs the map and knows its types. Having parse() use Map for the same logical structure makes the two halves of the same converter inconsistent.

A narrowly scoped @SuppressWarnings("unchecked") on the single cast line — as you did in ParseUtils — would acknowledge the one genuinely unchecked operation, preserve the Map<String, Object> type for the rest of the method, and keep parse() and render() symmetric. That is why I still think it is the better approach for this particular class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, there is an asymmetry there, but it's intended: render() calls put(String, Object), so it needs to know the type, while parse() uses only plain Object. The symmetry before came from parse() asserting a type that the compiler can't verify, which is exactly the thing to eliminate.

The cases in ParseUtils and Registry are cases where wildcards can't be used. It's not an open choice. The warnings could not be eliminated there, which is why I kept @SuppressWarnings there.

After this PR, there will be no instances of (Map<String, Object>) left in the codebase. Right now, this line, JiraServerInfoConverter.java:32, seems to be the only one in dispute. Combined with #48, this will eliminate all cases of (Map<String, Object>) as well as raw Map and List that were in the codebase. One aside is that the warnings in the generated code were also being passed onto all consumers like kojiji.

@github-actions

Copy link
Copy Markdown

Mend Scan Results

Status: ✅ No findings detected

SCA scan output



Identified 7 dependencies

Detected 0 vulnerabilities (0 Critical, 0 High, 0 Medium, 0 Low)





No Policy violations were detected

Project 'RWX' was updated, for more information, visit the Mend platform: https://ibmets.whitesourcesoftware.com/app/orgs/Enterprise%20Applications/applications/summary?project=760786cb-8ff0-436a-920e-71c3e5159dab
Or the Core UI: https://ibmets.whitesourcesoftware.com/Wss/WSS.html#!project;token=dd24f15fdadd4850a3f740fab7de870bd0a669b1a8314a679dbdd83fb69e1e21

Mend AI scan succeeded.

Support Token: 072aee4038d2f46a5845f46d41953f9b01789129114713
SAST scan output
*no findings*

Full logs and artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants